home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 21
/
Cream of the Crop 21 (Terry Blount) (October 1996).iso
/
comm
/
msged400.zip
/
src
/
date.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-07-22
|
15KB
|
615 lines
/*
* DATE.C
*
* Written on 30-Jul-90 by jim nutt. Changes on 10-Jul-94 by John Dennis.
* Released to the public domain.
*
* Parse various string date formats into a UNIX style timestamp.
*/
#include "msged.h"
#include "date.h"
#include "strextra.h"
#include "memextra.h"
static char *month[] =
{
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
static char *day[] =
{
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
static char *attr_tokens[] =
{
"yms", /* year msg */
"yno", /* year now */
"mms", /* month msg */
"mno", /* month now */
"dms", /* day msg */
"dno", /* day now */
"wms", /* weekday msg */
"wno", /* weekday now */
"tnm", /* time msg normal */
"tnn", /* time now normal */
"tam", /* time msg atime */
"tan", /* time now atime */
/* ------------- */
"ofn", /* orginal from name */
"off", /* original from first name */
"otn", /* original to name */
"otf", /* original to first name */
"osu", /* original subject */
"ooa", /* original origin address */
"oda", /* orginal destination address */
/* ------------- */
"fna", /* from name */
"ffn", /* from first name */
"fad", /* from address */
"tna", /* to name */
"tfn", /* to first name */
"tad", /* to address */
"sub", /* subject */
/* ------------- */
"una", /* user name */
"ufn", /* user first name */
"uad", /* user address */
"ceh", /* current echo tag */
"oeh", /* old echo tag */
/* ------------- */
"ims", /* iso date of msg */
"ino", /* iso date now */
"cms", /* 4-digit year of msg */
"cno", /* 4-digit year of now */
NULL
};
static int valid_date(struct tm *tms);
time_t parsedate(char *ds)
{
int t;
struct tm tm;
char work[80], *s;
if (ds == NULL || strlen(ds) == 0)
{
return 0;
}
memset(&tm, 0, sizeof tm);
strcpy(work, ds);
if (strchr(ds, '-') != NULL)
{ /* quickbbs style date */
s = strtok(work, "-");
if (s != NULL)
{
tm.tm_mon = atoi(s) - 1;
}
s = strtok(NULL, "-");
if (s != NULL)
{
tm.tm_mday = atoi(s);
}
s = strtok(NULL, " ");
if (s != NULL)
{
tm.tm_year = atoi(s);
}
s = strtok(NULL, ":");
if (s != NULL)
{
while (isspace(*s))
s++;
tm.tm_hour = atoi(s);
}
s = strtok(NULL, " ");
if (s != NULL)
{
tm.tm_min = atoi(s);
}
tm.tm_sec = 0;
}
else
{ /* fido style date */
s = strtok(work, " ");
if (s == NULL)
{
return 0;
}
if ((t = atoi(s)) == 0)
{ /* a usenet date */
s = strtok(NULL, " ");
if (s == NULL)
{
return 0;
}
t = atoi(s);
}
tm.tm_mday = t;
s = strtok(NULL, " ");
if (s == NULL)
{
return 0;
}
for (t = 0; t < 12; t++)
{
if (stricmp(s, month[t]) == 0)
{
break;
}
}
if (t == 12)
{
t = 1; /* WRA */
}
tm.tm_mon = t;
s = strtok(NULL, " ");
if (s == NULL)
{
return 0;
}
tm.tm_year = atoi(s);
s = strtok(NULL, ":");
if (s == NULL)
{
return 0;
}
while (isspace(*s))
{
s++;
}
tm.tm_hour = atoi(s);
s = strtok(NULL, ": \0");
if (s == NULL)
{
return 0;
}
tm.tm_min = atoi(s);
s = strtok(NULL, " ");
if (s != NULL)
{
tm.tm_sec = atoi(s);
}
tm.tm_isdst = -1;
}
#ifdef NEVER
tm.tm_hour += 5;
#endif
return mktime(&tm);
}
char *itime(time_t now)
{
struct tm *tm;
static char tmp[40];
tm = localtime(&now);
if (!tm || !valid_date((tm)))
{
sprintf(tmp, "%04d-%02d-%02d %02d:%02d:%02d",
1970, 1, 1, 0, 0, 0);
}
else
{
sprintf(tmp, "%04d-%02d-%02d %02d:%02d:%02d",
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
}
return tmp;
}
char *atime(time_t now)
{
struct tm *tm;
static char tmp[40];
tm = localtime(&now);
if (!tm || !valid_date((tm)))
{
sprintf(tmp, "%s %s %02d %04d %02d:%02d:%02d",
day[4], month[0], 1, 1970, 0, 0, 0);
}
else
{
sprintf(tmp, "%s %s %02d %04d %02d:%02d:%02d",
day[tm->tm_wday], month[tm->tm_mon], tm->tm_mday,
tm->tm_year + 1900, tm->tm_hour, tm->tm_min, tm->tm_sec);
}
return tmp;
}
char *mtime(time_t now)
{
struct tm *tm;
static char tmp[21];
tm = localtime(&now);
if (!tm || !valid_date((tm)))
{
sprintf(tmp, "%02d %s %02d %02d:%02d:%02d",
1, month[0], 70, 0, 0, 0);
}
else
{
sprintf(tmp, "%02d %s %02d %02d:%02d:%02d",
tm->tm_mday, month[tm->tm_mon], tm->tm_year,
tm->tm_hour, tm->tm_min, tm->tm_sec);
}
return tmp;
}
char *qtime(time_t now)
{
struct tm *tm;
static char tmp[20];
tm = localtime(&now);
if (!tm || !valid_date((tm)))
{
sprintf(tmp, "%s %02d %02d:%02d",
month[0], 1, 0, 0);
}
else
{
sprintf(tmp, "%s %02d %02d:%02d",
month[tm->tm_mon], tm->tm_mday, tm->tm_hour, tm->tm_min);
}
return tmp;
}
/* find_token - returns the token number or -1 if not found */
int find_token(char *token)
{
int i;
i = 0;
while (attr_tokens[i] != NULL)
{
if (stricmp(attr_tokens[i], token) == 0)
{
return i;
}
i++;
}
return -1;
}
/* Returns a pointer to the first name. Note: uses static memory. */
char *firstname(char *name)
{
char *s;
static char work[40];
memset(work, 0, sizeof work);
s = strchr(name, ' ');
if (s == NULL)
{
strcpy(work, name);
}
else
{
*s = '\0';
strcpy(work, name);
*s = ' ';
}
return work;
}
/* attrib_line - builds an attribution line */
char *attrib_line(msg * m, msg * old, int olda, char *format)
{
struct tm now, *tm;
char work[256], token[5], *t;
time_t n;
int num;
if (format == NULL)
{
return NULL;
}
memset(work, 0, sizeof work);
t = work;
n = time(NULL);
tm = localtime(&n);
now = *tm;
if (old)
{
tm = localtime(&(old->timestamp));
}
while (*format)
{
if (*format == '%')
{
format++;
switch (*format)
{